package clinic.ms.controller;

import clinic.ms.entity.Item;
import clinic.ms.entity.Pie;
import clinic.ms.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ItemController {
    @Autowired
    ItemService itemService = new ItemService();

    // 药品入库/添加药品 - get
    @GetMapping("/DrugInStorage")
    public String getDrugInStorage(){
        return "/DrugInStorage";
    }

    @GetMapping("/NewMenu")
    public String getNewMenu(){
        return "/NewMenu";
    }

    // 添加药品 - post
    @PostMapping("/addDrug")
    public ModelAndView addDrug(@RequestParam(value = "itemName",required = false) String itemName,
                         @RequestParam(value = "itemAliasName",required = false)String itemAliasName,
                         @RequestParam(value = "itemSpec",required = false)String itemSpec,
                         @RequestParam(value = "itemUnit",required = false)String itemUnit,
                         @RequestParam(value = "itemMadein",required = false)String itemMadein,
                         @RequestParam(value = "itemIdCard",required = false)String itemIdCard,
                         @RequestParam(value = "itemInPrice",required = false,defaultValue = "0")float itemInPrice,
                         @RequestParam(value = "itemPrice",required = false,defaultValue = "0")float itemPrice,
                         @RequestParam(value = "itemNums",required = false,defaultValue = "0")int itemNums,
                         @RequestParam(value = "itemType",required = false)String itemType,
                         @RequestParam(value = "itemRemarks",required = false)String itemRemarks){
        ModelAndView modelAndView = new ModelAndView("/DrugInStorage");
        Item item = new Item("#",itemName,itemAliasName,itemSpec,itemUnit,itemMadein,itemIdCard,itemInPrice,itemPrice,itemNums,itemType,itemRemarks);
        System.out.println(itemIdCard);
        String addResult = itemService.insertItem(item);
        if(addResult == null)  // 插入失败
            modelAndView.addObject("result", "插入失败，请重试!");
        else   // 插入成功，返回插入的药品编号
            modelAndView.addObject("result", "插入成功，药品编号："+addResult);
        return modelAndView;
    }

    // 显示全部信息
    @GetMapping("/DrugAdmin")
    public ModelAndView listDrug(){
        ModelAndView modelAndView = new ModelAndView("/DrugAdmin");
        List<Item> items;
        // 查询所有
        items = itemService.queryByNameAndNo("", "");
        modelAndView.addObject("items",items);
        return modelAndView;
    }

    // 跳转到编辑
    @GetMapping("/editDrug")
    public ModelAndView editDrug(@RequestParam("id")String itemNo){
        ModelAndView modelAndView = new ModelAndView("/editDrug");
        Item item = itemService.queryByNo(itemNo);
        modelAndView.addObject("item",item);
        return modelAndView;
    }

    // 编辑
    @PostMapping("editDrug")
    public ModelAndView editDrug(@RequestParam(value = "itemNo",required = false) String itemNo,
                                 @RequestParam(value = "itemName",required = false) String itemName,
                                 @RequestParam(value = "itemAliasName",required = false)String itemAliasName,
                                 @RequestParam(value = "itemSpec",required = false)String itemSpec,
                                 @RequestParam(value = "itemUnit",required = false)String itemUnit,
                                 @RequestParam(value = "itemMadein",required = false)String itemMadein,
                                 @RequestParam(value = "itemIdCard",required = false)String itemIdCard,
                                 @RequestParam(value = "itemInPrice",required = false,defaultValue = "0")float itemInPrice,
                                 @RequestParam(value = "itemPrice",required = false,defaultValue = "0")float itemPrice,
                                 @RequestParam(value = "itemNums",required = false,defaultValue = "0")int itemNums,
                                 @RequestParam(value = "itemType",required = false)String itemType,
                                 @RequestParam(value = "itemRemarks",required = false)String itemRemarks)
    {
        ModelAndView modelAndView = new ModelAndView("/editDrug");
        Item item = new Item(itemNo,itemName,itemAliasName,itemSpec,itemUnit,itemMadein,itemIdCard,itemInPrice,itemPrice,itemNums,itemType,itemRemarks);
        if(itemService.updateByNo(item))
            modelAndView.addObject("item",itemService.queryByNo(itemNo));
        else modelAndView.addObject("item",item);
        return modelAndView;
    }

    // 删除
    @PostMapping("/deleteDrug")
    public ModelAndView deleteDrug(String id) {

        try {
            itemService.delete(id);
        } catch (Exception e) {
            System.out.println(e);
        }
        return new ModelAndView(new RedirectView("/DrugAdmin"));
    }

    //查询
    @PostMapping("/searchDrug")
    public ModelAndView searchDrug(String no,String name){
        List<Item> items;
        //System.out.println("++++"+no+name);
        items = itemService.queryByNameAndNo(no, name);
        ModelAndView modelAndView = new ModelAndView("/DrugAdmin");
        modelAndView.addObject("items",items);
        return modelAndView;
    }

    //药品入库
    @PostMapping("/addNum")
    public ModelAndView addNum(int num,String id){
        itemService.updateNumsByNo(id,num);
        return new ModelAndView(new RedirectView("/DrugAdmin"));
    }

    @GetMapping("/itemChart")
    public ModelAndView itemChart(){
        ModelAndView modelAndView = new ModelAndView("/itemChart");
        List<Pie> pieList;
        pieList = itemService.statisticsType();
        modelAndView.addObject("size",pieList.size());
        modelAndView.addObject("pieList",pieList);
        return modelAndView;
    }



}


